package me.yuzho.study.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/** 自定义ThreadPoolExecutor, 使用DaemonThreadFactory作为默认的线程生成器
 * @author yzhou-citrix
 *
 */
public class DaemonThreadPoolExecutor extends ThreadPoolExecutor {

    public DaemonThreadPoolExecutor() {
        super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, 
                new SynchronousQueue<Runnable>(),
                new DaemonThreadFactory());
    }
    
    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String [] args) throws InterruptedException {
        
        ExecutorService exec = new DaemonThreadPoolExecutor();
         
        //for(int i=0; i<10; i++) {
        IntStream.range(0, 10).forEach(i -> {
            exec.execute(new SimpleDaemons());
        });
        //}
        exec.shutdown();
        
        System.out.println("All daemons started");
        TimeUnit.MILLISECONDS.sleep(500);
    }
}
